﻿using System;
using System.IO;
using System.Text;

namespace Tester
{
    public 
    static class Logger
    {
        private static readonly string FileName = "tester" + DateTime.Today.ToShortDateString() + ".txt";

        static Logger()
        {
            Enabled = true;
            Log("Test start");
        }

        private static FileStream fileStream;

		public static bool Enabled { get; set; }

// ReSharper disable MethodOverloadWithOptionalParameter
        public static void Log(string format, params object[] arg)
// ReSharper restore MethodOverloadWithOptionalParameter
        {
			Log(string.Format(System.Globalization.CultureInfo.CurrentCulture, format, arg));
        }

        public static void Log(string message)
        {
            if (!Enabled) return;
            if (fileStream == null)
            {
                fileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            }
            int length = message.IndexOf("\n\r", StringComparison.Ordinal);
            string modifiedMessage = length > 0 ? message.Substring(0, length) : message;

            byte[] byteData = Encoding.ASCII.GetBytes(
                //DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss ") + 
                modifiedMessage + "\n\r");
            if (fileStream != null)
            {
                fileStream.Write(byteData, 0, byteData.Length);
                fileStream.Flush();
            }

            if (length <= 0) return;
            Log(message.Substring(length + 1, message.Length - length - 1));
        }

        public static void Close()
        {
            //fileStream.Close();
        }
    }
}